package com.my;

import javax.servlet.*;
import java.io.IOException;
import java.util.Enumeration;

/**
 * Created by YJH on 2017/6/22 21:36.
 */
public class HelloServlet implements Servlet {

    public HelloServlet() {
        System.out.println("HelloServlet构造器");
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("HelloServlet → init()");

        String user = servletConfig.getInitParameter("user");
        System.out.println("user = " + user);
        Enumeration<String> names = servletConfig.getInitParameterNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            System.out.println("name = " + name + "; " + servletConfig.getInitParameter(name));
        }

        String servletName = servletConfig.getServletName();
        System.out.println("servlet名字：" + servletName);

        //获取ServletContext对象
        ServletContext servletContext = servletConfig.getServletContext();
        String driver = servletContext.getInitParameter("driver");
        System.out.println("driver--->" + driver);

        Enumeration<String> names2 = servletContext.getInitParameterNames();
        while (names2.hasMoreElements()) {
            String name = names2.nextElement();
            System.out.println(name + "--->" + servletContext.getInitParameter(name));
        }

        //获取当前WEB应用的某一文件在服务器上的绝对路径，而不是部署前的路径
        String realPath = servletContext.getRealPath("/uuu.txt");
        System.out.println("uuu.txt文件在服务器上的绝对路径--->" + realPath);

        System.out.println("uuu.txt文件部署前的路径--->" + servletContext.getContextPath());
    }

    @Override
    public ServletConfig getServletConfig() {
        System.out.println("HelloServlet → getServletConfig()");
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("HelloServlet → service()");
    }

    @Override
    public String getServletInfo() {
        System.out.println("HelloServlet → getServletInfo()");
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("HelloServlet → destroy()");
    }
}
